package hackbard.piratradar;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.view.Gravity;
import android.widget.Toast;






public class GsmLoc  extends Service{
	
GsmLoc() {
		super();
		// TODO Auto-generated constructor stub
	}



private TelephonyManager tm;

@SuppressWarnings("unused")
public double[] getgsm(Context ctx)
	{       

		long lat = 0,lng = 0;
//	    Context Context = getBaseContext();
		//getBaseContext();
		tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
         //p = loc.get(tm);
		GsmCellLocation location = (GsmCellLocation) tm.getCellLocation();
	    int cid = location.getCid();
	    int lac = location.getLac();
	    String urlString = "http://www.google.com/glm/mmap"; 
	    try
	    	{
	    	URL url = new URL(urlString); 
	    	URLConnection conn = url.openConnection();
	    	HttpURLConnection httpConn = (HttpURLConnection) conn;        
	    	httpConn.setRequestMethod("POST");
	    	httpConn.setDoOutput(true); 
	    	httpConn.setDoInput(true);
	    	httpConn.connect(); 
	    	OutputStream outputStream = httpConn.getOutputStream();
	    	WriteData(outputStream, cid, lac);
	    	InputStream inputStream = httpConn.getInputStream();  
	    	DataInputStream dataInputStream = new DataInputStream(inputStream);
	    	dataInputStream.readShort();
	    	dataInputStream.readByte();
	    	int code = dataInputStream.readInt();
	    	lat = dataInputStream.readInt();
	    	lng = dataInputStream.readInt();
	    	dataInputStream.readInt();
	    	dataInputStream.readInt();
	    	dataInputStream.readUTF();
	    	}  
       catch(Exception e)
	    	{
        	String strResult = "Error!\n" + e.getMessage();
        	Toast t = Toast.makeText(getBaseContext(), strResult,
        		      Toast.LENGTH_LONG);
        	t.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        	t.show();
	    	}
       		double pairs[] = new double[2];
   			pairs[0] = (double)lat / 1000000; 
   			pairs[1] = (double)lng / 1000000; 
   			return pairs;
       }





	private void WriteData(OutputStream out, int cellID, int lac) 
	    throws IOException
	    {    	
	        DataOutputStream dataOutputStream = new DataOutputStream(out);
	        dataOutputStream.writeShort(21);
	        dataOutputStream.writeLong(0);
	        dataOutputStream.writeUTF("en");
	        dataOutputStream.writeUTF("Android");
	        dataOutputStream.writeUTF("1.0");
	        dataOutputStream.writeUTF("Web");
	        dataOutputStream.writeByte(27);
	        dataOutputStream.writeInt(0);
	        dataOutputStream.writeInt(0);
	        dataOutputStream.writeInt(3);
	        dataOutputStream.writeUTF("");

	        dataOutputStream.writeInt(cellID);  
	        dataOutputStream.writeInt(lac);     

	        dataOutputStream.writeInt(0);
	        dataOutputStream.writeInt(0);
	        dataOutputStream.writeInt(0);
	        dataOutputStream.writeInt(0);
	        dataOutputStream.flush();    	
	    }





	@Override
	public boolean bindService(Intent service, ServiceConnection conn, int flags) {
		// TODO Auto-generated method stub
		return false;
	}





	@Override
	public int checkCallingOrSelfPermission(String permission) {
		// TODO Auto-generated method stub
		return 0;
	}





	@Override
	public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
		// TODO Auto-generated method stub
		return 0;
	}





	@Override
	public int checkCallingPermission(String permission) {
		// TODO Auto-generated method stub
		return 0;
	}





	@Override
	public int checkCallingUriPermission(Uri uri, int modeFlags) {
		// TODO Auto-generated method stub
		return 0;
	}





	@Override
	public int checkPermission(String permission, int pid, int uid) {
		// TODO Auto-generated method stub
		return 0;
	}





	@Override
	public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
		// TODO Auto-generated method stub
		return 0;
	}





	@Override
	public int checkUriPermission(Uri uri, String readPermission,
			String writePermission, int pid, int uid, int modeFlags) {
		// TODO Auto-generated method stub
		return 0;
	}





	@Override
	public void clearWallpaper() throws IOException {
		// TODO Auto-generated method stub
		
	}





	@Override
	public Context createPackageContext(String packageName, int flags)
			throws NameNotFoundException {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public String[] databaseList() {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public boolean deleteDatabase(String name) {
		// TODO Auto-generated method stub
		return false;
	}





	@Override
	public boolean deleteFile(String name) {
		// TODO Auto-generated method stub
		return false;
	}





	@Override
	public void enforceCallingOrSelfPermission(String permission, String message) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void enforceCallingOrSelfUriPermission(Uri uri, int modeFlags,
			String message) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void enforceCallingPermission(String permission, String message) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void enforceCallingUriPermission(Uri uri, int modeFlags,
			String message) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void enforcePermission(String permission, int pid, int uid,
			String message) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void enforceUriPermission(Uri uri, int pid, int uid, int modeFlags,
			String message) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void enforceUriPermission(Uri uri, String readPermission,
			String writePermission, int pid, int uid, int modeFlags,
			String message) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public String[] fileList() {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public Context getApplicationContext() {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public ApplicationInfo getApplicationInfo() {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public AssetManager getAssets() {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public File getCacheDir() {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public ClassLoader getClassLoader() {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public ContentResolver getContentResolver() {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public File getDatabasePath(String name) {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public File getDir(String name, int mode) {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public File getFileStreamPath(String name) {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public File getFilesDir() {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public Looper getMainLooper() {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public PackageManager getPackageManager() {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public String getPackageName() {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public Resources getResources() {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public SharedPreferences getSharedPreferences(String name, int mode) {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public Object getSystemService(String name) {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public Theme getTheme() {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public Drawable getWallpaper() {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public int getWallpaperDesiredMinimumHeight() {
		// TODO Auto-generated method stub
		return 0;
	}





	@Override
	public int getWallpaperDesiredMinimumWidth() {
		// TODO Auto-generated method stub
		return 0;
	}





	@Override
	public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public FileInputStream openFileInput(String name)
			throws FileNotFoundException {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public FileOutputStream openFileOutput(String name, int mode)
			throws FileNotFoundException {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public SQLiteDatabase openOrCreateDatabase(String name, int mode,
			CursorFactory factory) {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public Drawable peekWallpaper() {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public Intent registerReceiver(BroadcastReceiver receiver,
			IntentFilter filter) {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public Intent registerReceiver(BroadcastReceiver receiver,
			IntentFilter filter, String broadcastPermission, Handler scheduler) {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public void removeStickyBroadcast(Intent intent) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void revokeUriPermission(Uri uri, int modeFlags) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void sendBroadcast(Intent intent) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void sendBroadcast(Intent intent, String receiverPermission) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void sendOrderedBroadcast(Intent intent, String receiverPermission,
			BroadcastReceiver resultReceiver, Handler scheduler,
			int initialCode, String initialData, Bundle initialExtras) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void sendStickyBroadcast(Intent intent) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void setTheme(int resid) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void setWallpaper(Bitmap bitmap) throws IOException {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void setWallpaper(InputStream data) throws IOException {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void startActivity(Intent intent) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public boolean startInstrumentation(ComponentName className,
			String profileFile, Bundle arguments) {
		// TODO Auto-generated method stub
		return false;
	}





	@Override
	public ComponentName startService(Intent service) {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public boolean stopService(Intent service) {
		// TODO Auto-generated method stub
		return false;
	}





	@Override
	public void unbindService(ServiceConnection conn) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void unregisterReceiver(BroadcastReceiver receiver) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
}
